package de.windeln.marketing.validation;

import org.joda.time.DateTime;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import de.windeln.marketing.campaign.costinterval.model.CostintervalClientObject;

@Component
public class CostIntervalStartAfterEnd implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return clazz == CostintervalClientObject.class;
	}

	@Override
	public void validate(Object target, Errors errors) {
		CostintervalClientObject cico = (CostintervalClientObject) target;
		if(new DateTime(cico.getCostStart()).isAfter(new DateTime(cico.getCostEnd()))){
			errors.rejectValue("costStart", "Wrong Dates", "Start Date is not allowed to be after End Date!");
			errors.rejectValue("costEnd", "Wrong Dates", "End Date is not allowed to be before Start Date!");
		}
	}

}
